﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using PRET.ReportDataDefine.UIData;
using PRET.ReportControlLib.ServiceReference;

namespace PRET.ReportControlLib.ReportView
{
    public partial class JHJReportView : UserControl
    {
        private List<OrderCheckInfo> orderList = new List<OrderCheckInfo>();
        private PRETWebServiceClient adapter = null;
        private List<Microsoft.Reporting.WinForms.ReportViewer> reportViewList = new List<Microsoft.Reporting.WinForms.ReportViewer>();
        private List<TabPage> tabPageList = new List<TabPage>();

        public JHJReportView()
        {
            InitializeComponent();
            adapter = new PRETWebServiceClient();
            this.checkTimePanel.correctTimeEvent += checkTimePanel_correctTimeEvent;
            this.checkTimePanel.ignoreTimeEvent += checkTimePanel_ignoreTimeEvent;
            this.checkTimePanel.showReportEvent += checkTimePanel_showReportEvent;
        }

        void checkTimePanel_showReportEvent(object sender, Controls.ShowReportViewEventArgs args)
        {
            try
            {
                this.checkTimePanel.EnableControls(false);
                OrderCheckInfo order = args.Order;
                if (order == null)
                    return;

                if (this.adapter == null)
                {
                    MessageBox.Show("JHJReportView 没有初始化Server Adapter!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                // 清空已有的数据
                foreach (Microsoft.Reporting.WinForms.ReportViewer item in this.reportViewList)
                    item.LocalReport.DataSources.Clear(); //clear report
                this.reportTabControl.Controls.Clear();

                // 从后台获取干燥均化机数据
                JHJReport[] dataList = this.adapter.GetJHJReport(order);
                if (dataList == null)
                {
                    string message = string.Format("读取订单{0}发生错误!", order.OrderID);
                    MessageBox.Show(message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                for (int i = 0; i < dataList.Length; i++)
                {
                    if (dataList[i] != null)
                    {
                        if (i >= tabPageList.Count)
                        {
                            NewReportViewTabPage(i + 1);
                        }
                        Microsoft.Reporting.WinForms.ReportDataSource summaryDataset = new Microsoft.Reporting.WinForms.ReportDataSource("JHJSummaryDS");
                        this.reportViewList[i].LocalReport.DataSources.Add(summaryDataset);
                        List<JHJSummary> list = new List<JHJSummary> { dataList[i].Summary };
                        summaryDataset.Value = list;

                        Microsoft.Reporting.WinForms.ReportDataSource detailsDataset = new Microsoft.Reporting.WinForms.ReportDataSource("JHJDetailInfoDS");
                        this.reportViewList[i].LocalReport.DataSources.Add(detailsDataset);
                        if (dataList[i].DataList != null)
                            detailsDataset.Value = dataList[i].DataList;

                        this.reportTabControl.Controls.Add(this.tabPageList[i]);
                        this.reportViewList[i].LocalReport.Refresh();
                        this.reportViewList[i].RefreshReport();
                    }
                    else
                    {
                        string message = string.Format("系统数据库中没有订单'{0}'的记录!", order.OrderID);
                        MessageBox.Show(message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                string msg = string.Format("JHJReportView.checkTimePanel_showReportEvent() Failed: {0}.\r\nDetail: {1}", ex.Message, ex.StackTrace);
                MessageBox.Show(msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            { this.checkTimePanel.EnableControls(true); }
        }

        void checkTimePanel_correctTimeEvent(object sender, ReportControlLib.Controls.CorrectOrderTimeEventArgs e)
        {
 	        try
            {
                this.checkTimePanel.EnableControls(false);
                this.adapter.CorrectJHJOrderTime(e.Order);
            }
            catch (Exception)
            {
                throw new NotImplementedException();
            }
            finally
            {
                this.checkTimePanel.EnableControls(false);
            }
        }

        void checkTimePanel_ignoreTimeEvent(object sender, ReportControlLib.Controls.CorrectOrderTimeEventArgs e)
        {
            try
            {
                this.checkTimePanel.EnableControls(false);
                this.adapter.IgnoreERPOrderTime(e.Order);
            }
            catch (Exception)
            {
                throw new NotImplementedException();
            }
            finally
            { this.checkTimePanel.EnableControls(true); }
        }

        public void SetOrderList()
        {
            OrderCheckInfo[] list = this.adapter.GetJHJOrderList();
            if (list != null && list.Length > 0)
                this.checkTimePanel.InitializeOrderIDPanel(list.ToList<OrderCheckInfo>());
        }

        //public void ShowReportView()
        //{
        //    OrderCheckInfo order = this.checkTimePanel.GetOrderCheckInfo();
        //    if (order == null)
        //        return;

        //    if (this.adapter == null)
        //    {
        //        MessageBox.Show("JHJReportView 没有初始化Server Adapter!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //        return;
        //    }

        //    // 清空已有的数据
        //    foreach (Microsoft.Reporting.WinForms.ReportViewer item in this.reportViewList)
        //        item.LocalReport.DataSources.Clear(); //clear report
        //    this.reportTabControl.Controls.Clear();

        //    // 从后台获取干燥均化机数据
        //    JHJReport[] dataList = this.adapter.GetJHJReport(order);
        //    if (dataList == null)
        //    {
        //        string message = string.Format("读取订单{0}发生错误!", order.OrderID);
        //        MessageBox.Show(message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //        return;
        //    }
        //    for (int i = 0; i < dataList.Length; i++)
        //    {
        //        if (dataList[i] != null)
        //        {
        //            if (i >= tabPageList.Count)
        //            {
        //                NewReportViewTabPage(i + 1);
        //            }
        //            Microsoft.Reporting.WinForms.ReportDataSource summaryDataset = new Microsoft.Reporting.WinForms.ReportDataSource("JHJSummaryDS");
        //            this.reportViewList[i].LocalReport.DataSources.Add(summaryDataset);
        //            List<JHJSummary> list = new List<JHJSummary> { dataList[i].Summary };
        //            summaryDataset.Value = list;

        //            Microsoft.Reporting.WinForms.ReportDataSource detailsDataset = new Microsoft.Reporting.WinForms.ReportDataSource("JHJDetailInfoDS");
        //            this.reportViewList[i].LocalReport.DataSources.Add(detailsDataset);
        //            detailsDataset.Value = dataList[i].DataList;

        //            this.reportTabControl.Controls.Add(this.tabPageList[i]);
        //            this.reportViewList[i].LocalReport.Refresh();
        //            this.reportViewList[i].RefreshReport();
        //        }
        //        else
        //        {
        //            string message = string.Format("系统数据库中没有订单'{0}'的记录!", order.OrderID);
        //            MessageBox.Show(message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //        }
        //    }
        //}

        private void NewReportViewTabPage(int index)
        {
            // 初始化JHJReportViewer
            Microsoft.Reporting.WinForms.ReportViewer JHJReportViewer = new Microsoft.Reporting.WinForms.ReportViewer();
            JHJReportViewer.AutoSize = true;
            JHJReportViewer.Dock = System.Windows.Forms.DockStyle.Fill;
            Microsoft.Reporting.WinForms.ReportDataSource reportDataSource = new Microsoft.Reporting.WinForms.ReportDataSource();
            reportDataSource.Name = "JHJReportDS";
            reportDataSource.Value = null;
            JHJReportViewer.LocalReport.DataSources.Add(reportDataSource);
            JHJReportViewer.LocalReport.ReportEmbeddedResource = "PRET.ReportControlLib.RDLC.JHJReport.rdlc";
            JHJReportViewer.Location = new System.Drawing.Point(3, 3);
            JHJReportViewer.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
            JHJReportViewer.Name = "JHJReportViewer" + index.ToString();
            JHJReportViewer.Size = new System.Drawing.Size(1005, 200);
            reportViewList.Add(JHJReportViewer);
            // 初始化TabPage
            TabPage tabPage = new TabPage();
            tabPage.SuspendLayout();
            tabPage.Controls.Add(JHJReportViewer);
            tabPage.Location = new System.Drawing.Point(4, 4);
            tabPage.Name = "tabPage" + index.ToString();
            tabPage.Padding = new System.Windows.Forms.Padding(3);
            tabPage.Size = new System.Drawing.Size(1011, 206);
            tabPage.Text = "均化机报表" + index.ToString();
            tabPage.UseVisualStyleBackColor = true;
            tabPage.ResumeLayout(false);
            tabPage.PerformLayout();
            tabPageList.Add(tabPage);
        }
    }
}
